package com.hundsun.fcp.webplugin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.hundsun.fcp.common.utils.StringUtils;

public class FileUtil {
	public static String formatPath(String pFilePath) {
		return pFilePath.replace("\\", "/");
	}

	/**
	 * 判断目录是否存在
	 * @param pFolderPath
	 * @return
	 */
	public static boolean isFolderExisted(String pFolderPath) {
		File _File = new File(pFolderPath);
		return (_File.isDirectory()) && (_File.exists());
	}

	/**
	 * 创建目录
	 * @param pFolderPath
	 * @return
	 */
	public static boolean makeDirectories(String pFolderPath) {
		return new File(pFolderPath).mkdirs();
	}
	
	/**
	 * 从地址中获取文件名称
	 * @param path
	 * @return
	 */
	public static String getNameFromPath (String path) {
		if (StringUtils.isEmpty(path)) {
			return "";
		}
		String tempUrl = path.replace("\\", "/");// 服务部署在windows上.则文件分隔符是\.所以要将\ 替换成/
		int index = tempUrl.lastIndexOf(File.separator);
		return tempUrl.substring(index + 1);
	}
	
	public static String getInputStream(InputStreamReader pReader){
		 BufferedReader bufReader = null ;
         try {
             bufReader = new BufferedReader(pReader);
             String line="";
             String Result="";
             while((line = bufReader.readLine()) != null)
                 Result += line;
             return Result;
         } catch (Exception e) {
             e.printStackTrace();
         }finally{ 
        	 try {
				if(bufReader!=null){
					 bufReader.close() ;
				 }
			} catch (IOException e) {
				e.printStackTrace();
			}
         }
         return null ;
	}
}
